<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Post extends CI_Controller{
	
	var $data;

	public function __construct(){
		parent::__construct();
		
		$this->load->model("m_kategori");
		$this->load->model("m_post");

	}
	
	public function index(){
		die("Halloo");
	}

	public function myPost($status = NULL, $message = NULL){
		$idUser = $this->arrUserSess["id_user"];
		$data["arrBerita"] = $this->m_post->getMyPost($idUser);
		$data['status'] = $status;
		$data['message'] = $message;
		$data["title"] = "My Post";
		$this->paint("my_post.php", $data);
	}

	public function createPost(){
		$data["title"] = "New Post";
		$data["page"] = 'new_post';
		$data["arrKategori"] = $this->m_kategori->getAllKategori();
		$this->load->view("/backend/dashboard", $data);
	}

	public function editPost($idPost){
		$data["title"] = "Edit Post";
		$data["arrKategori"] = $this->m_kategori->getAllKategori();
		$data["arrBerita"] = $this->m_post->getPostById($idPost);
		$this->paint("edit_post.php", $data);
	}

	public function deletePost($idPost){
		if($this->m_post->deletePost($idPost)){
			$status = "alert-success";
			$message = "Delete Berhasil";

		}else{
			$status = "alert-danger";
			$message = "Delete Gagal";
		}
		$this->myPost($status, $message);
	}

	public function lists($status = NULL, $message = NULL){
		$data["title"] = "All Post";
		$data["page"] = 'post';
		$data["arrPost"] = $this->m_post->getAll();
		$this->load->view("/backend/dashboard", $data);
	}

	public function viewPost($idPost){
		$data["title"] = "All Post";
		$data["arrBerita"] = $this->m_post->getPostById($idPost);
		$this->paint("view_post.php", $data);
	}

	public function approve($strId){
		$arrParam = array(
			1,
			$strId
		);
		if($this->m_post->approvalPost($arrParam)){
			$status = "alert-success";
			$message = "Approved";
		}else{
			$status = "alert-danger";
			$message = "Failed Approving";
		}
		$this->lists($status, $message);
	}

	public function unapprove($strId){
		$arrParam = array(
			0,
			$strId
		);
		if($this->m_post->approvalPost($arrParam)){
			$status = "alert-success";
			$message = "Post Rejected";
		}else{
			$status = "alert-danger";
			$message = "Failed Rejecting";
		}
		$this->lists($status, $message);
	}

	public function ajaxProcess(){
		$idUser = $this->arrUserSess["id_user"];
		$username = $this->arrUserSess["username"];
		$data = array();
		// echo $this->input->post("content");
		// echo $this->input->post("title");
		$status = $this->input->post("status");
		if(!empty($_FILES) && !($status=="edit") ){  
			$error = false;
			$files = array();
			$uploaddir = './photos/post/';
			foreach($_FILES as $file)
			{
				$name    = basename($file['name']);
				$ext     = end(explode('.', $name));
				$name = $username .".".$this->m_post->getMaxId();
				if(move_uploaded_file($file['tmp_name'], $uploaddir . $name .".".$ext))
				{
					$arrParam = array(
						$name,
						$this->input->post("title"),
						$this->input->post("content"),
						$this->input->post("kategori"),
						$idUser,
						base_url("photos/post")."/".$name .".".$ext		
					);
					if($this->m_post->insertPost($arrParam)){
						$files[] = $uploaddir .$file['name'];	
					}else{
						$error = true;		
					}
					
				}
				else
				{
				    $error = true;
				}
			}
			$data = ($error) ? array('error' => 'There was an error uploading your files') : array('files' => $files);
		} else if(!empty($_FILES) && $status=="edit"){
			$error = false;
			$files = array();
			$uploaddir = './photos/post/';
			foreach($_FILES as $file)
			{
				$name    = basename($file['name']);
				$ext     = end(explode('.', $name));
				$name =  $username .".".$this->input->post("id_berita");
				if(move_uploaded_file($file['tmp_name'], $uploaddir . $name .".".$ext))
				{
					$arrParam = array(
						$this->input->post("title"),
						$this->input->post("content"),
						$this->input->post("kategori"),
						base_url("photos/post")."/".$name .".".$ext,
						$this->input->post("id_berita")
					);
					if($this->m_post->updatePostWithPicture($arrParam)){
						$files[] = $uploaddir .$file['name'];	
					}else{
						$error = true;		
					}
					
				}
				else
				{
				    $error = true;
				}
			}
			$data = ($error) ? array('error' => 'There was an error uploading your files') : array('files' => $files);
		} else if(empty($_FILES) && $status=="edit"){
			$arrParam = array(
				$this->input->post("title"),
				$this->input->post("content"),
				$this->input->post("kategori"),
				$this->input->post("id_berita")
			);
			if(!$this->m_post->updatePost($arrParam)){
				$error = true;
			}
		}
		echo json_encode($data);
	}
}
/* End of file post.php */
/* Location: .controllers/backend/post.php */